package com.feifei.controller;

import com.feifei.service.CityService;
import com.feifei.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

@CrossOrigin
@Controller
public class CityController {

    @Autowired
    CityService cityService;

    @ResponseBody
    @RequestMapping("/city/saveCity")
    public Result saveCity(String cityname) {
        int id=cityService.saveCity(cityname);
        Result result=new Result();
        if(id>0){
            result.setData(id);
            result.setMsg("地址存放成功");
            result.setStatus(true);
        }else{result.setMsg("地址存放失败");
            result.setStatus(false);
        }
        return result;
    }
    @ResponseBody
    @RequestMapping("/city/getProvince")
    public Result getProvince() throws IOException {
        StringBuilder content = new StringBuilder();
        String filePath = "D:\\Java\\workspace\\rowdfunding_system\\src\\main\\resources\\json\\province.json";
        BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(filePath), "UTF-8"));
        String line;
        while ((line = reader.readLine()) != null) {
            content.append(line);
        }
        reader.close();
        Result result=new Result();
        result.setData(content);
        result.setMsg("获取省份成功");
        result.setStatus(true);
        return result;
    }
    @ResponseBody
    @RequestMapping("/city/getCity")
    public Result getCity() throws IOException {
        StringBuilder content = new StringBuilder();
        String filePath = "D:\\Java\\workspace\\rowdfunding_system\\src\\main\\resources\\json\\city.json";
        BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(filePath), "UTF-8"));
        String line;
        while ((line = reader.readLine()) != null) {
            content.append(line);
        }
        reader.close();
        Result result=new Result();
        result.setData(content);
        result.setMsg("获取城市成功");
        result.setStatus(true);
        return result;
    }
}
